package Vista;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import Controlador.Sistema;
import Negocio.ItemPlan;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AltaPlanBase extends javax.swing.JFrame {
	public static String TITLE = "Alta de Plan Base";
	/**
	* Auto-generated main method to display this JFrame
	*/
	private Sistema sistema;
	private JButton jbAddServicio;
	private JButton jBtnGrabar;
	private JTextField txtcostoBase;
	private JLabel lblCBase;
	private JTextField txtNumero;
	private JLabel lblNumero;
	private JTextField txtNombre;
	private JLabel lblNombre;
	private JLabel lblUltAgrega;
	private JButton btnServiciosSeleccionados;

	public AltaPlanBase(Sistema s) {
		// TODO Auto-generated constructor stub
		super(TITLE);
		sistema=s;
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				lblNumero = new JLabel();
				getContentPane().add(lblNumero);
				lblNumero.setText("Numero:");
				lblNumero.setBounds(36, 37, 135, 16);
			}
			{
				txtNumero = new JTextField();
				getContentPane().add(txtNumero);
				txtNumero.setBounds(124, 34, 222, 23);
			}
			{
				lblNombre = new JLabel();
				getContentPane().add(lblNombre);
				lblNombre.setText("Nombre:");
				lblNombre.setBounds(36, 65, 83, 16);
			}
			{
				txtNombre = new JTextField();
				getContentPane().add(txtNombre);
				txtNombre.setBounds(124, 62, 222, 23);
			}
			
			{
				lblCBase = new JLabel();
				getContentPane().add(lblCBase);
				lblCBase.setText("Costo Base:");
				lblCBase.setBounds(36, 96, 132, 16);
			}
			{
				txtcostoBase = new JTextField();
				getContentPane().add(txtcostoBase);
				txtcostoBase.setBounds(124, 93, 222, 23);
			}
			{
				jBtnGrabar = new JButton();
				getContentPane().add(jBtnGrabar);
				jBtnGrabar.setText("Grabar");
				jBtnGrabar.setBounds(30, 158, 142, 23);
				jBtnGrabar.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						if (controlCampos()){
							try {
								
								
						sistema.altaPlanBase(Integer.parseInt(txtNumero.getText()),Float.parseFloat(txtcostoBase.getText()),"");
						jBtnGrabar.setEnabled(false);
						txtNumero.setEnabled(false);
						txtNombre.setEnabled(false);
						txtcostoBase.setEnabled(false);
						jbAddServicio.setEnabled(true);
						
							} catch (Exception ex) {
								
							}
							}
							
						}

					});
			}
			{
				jbAddServicio = new JButton();
				getContentPane().add(jbAddServicio);
				jbAddServicio.setText("AgregarServicio");
				jbAddServicio.setBounds(183, 158, 168, 23);
				jbAddServicio.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0){
						if (controlCampos()){
						try {
							
							
							//SeleccionarServicio serviciosWindow = new SeleccionarServicio();
							//serviciosWindow.setLocationRelativeTo(null);
							//serviciosWindow.setVisible(true);	

							SeleccionarServicio serviciosWindow = new SeleccionarServicio(sistema);
							serviciosWindow.setLocationRelativeTo(null);
							serviciosWindow.setVisible(true);	


							ItemPlan aux = sistema.getItemPlanAux();
							if (aux!=null){
								sistema.agregarItemPlan(Integer.parseInt(txtNumero.getText()), aux);
								lblUltAgrega.setText(aux.getServicio().getNombre().concat(" se ha agregado."));
							}

							
							
						} catch (Exception ex) {
			
						}
						}
						
					}
				});

			}
			{
				lblUltAgrega = new JLabel();
				getContentPane().add(lblUltAgrega);
				lblUltAgrega.setBounds(36, 131, 309, 21);
			}
			{
				btnServiciosSeleccionados = new JButton();
				getContentPane().add(btnServiciosSeleccionados);
				btnServiciosSeleccionados.setText("Servicios Seleccionados");
				btnServiciosSeleccionados.setBounds(30, 192, 321, 23);
				btnServiciosSeleccionados.setEnabled(false);
				btnServiciosSeleccionados.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent arg0){
						if (controlCampos()){
						try {
							
							
							ServiciosAgregados serviciosWindow = new ServiciosAgregados(sistema, sistema.buscarPlan(Integer.parseInt(txtNumero.getText())));
							serviciosWindow.setLocationRelativeTo(null);
							serviciosWindow.setVisible(true);	

							
							
							
						} catch (Exception ex) {
			
						}
						}
						
					}
				});
			}
			pack();
			this.setSize(400, 265);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private boolean controlCampos() {
		JFrame j1 = new JFrame();		
		@SuppressWarnings("unused")
		int numero;
		
		if (txtNumero.getText().equals("")) {																					
			JOptionPane.showMessageDialog(j1,
					"Debe ingresar el Nro de Plan",
					"Omision",
					JOptionPane.WARNING_MESSAGE);	
			txtNumero.requestFocus(true);
			return false;
		}
		if (txtcostoBase.getText().equals("")) {																					
			JOptionPane.showMessageDialog(j1,
					"Debe ingresar el Costo Base",
					"Omision",
					JOptionPane.WARNING_MESSAGE);	
			txtcostoBase.requestFocus(true);
			return false;
		}
		if (txtNombre.getText().equals("")) {																					
			JOptionPane.showMessageDialog(j1,
					"Debe ingresar el Nombre del Plan",
					"Omision",
					JOptionPane.WARNING_MESSAGE);	
			txtNombre.requestFocus(true);
			return false;
		}

		
		return true;
	}


}
